<?php

require_once ('IPdfService.php');
require_once ('BaseService.php');
require_once 'dao/PdfDao.php';
require_once 'util/ServiceUtil.php';
require_once 'util/StringUtil.php';

class PdfService extends BaseService implements IPdfService {
	public static $NAME = "PdfService";
	protected $pdfDao;
	
	public function PdfService()
	{
		$this->initializeDB();
		$this->pdfDao = new PdfDao();
	}
	
	public function create(PdfDao $object) 
	{
		if($object)
		{
			try
			{
				$this->sql = "INSERT INTO pdf(description,title,url) VALUES('$object->description','$object->title',
													'$object->url')";
				$this->query = mysql_query($this->sql,$this->myDb);
				return $this->checkQueryResult(mysql_affected_rows($this->myDb));
			}catch(Exception $exc)
			{
				return $exc;
			}
		}
		return NULL;
	}
	
	public function delete($id) 
	{
		if($id)
		{
			try
			{
				$this->sql = "DELETE FROM pdf WHERE id = '$id'";
				$this->query = mysql_query($this->sql,$this->myDb);
				return $this->checkQueryResult(mysql_affected_rows($this->myDb));
			}catch(Exception $exc)
			{
				return $exc;
			}
		}
		return NULL;
	}
	
	public function read($id) 
	{
		if($id==NULL||$id==-1)
		{
			$this->sql = "SELECT * FROM pdf";
		}else
		{
			$this->sql = "SELECT * FROM pdf WHERE id='$id'";
		}
		try
		{
			$this->query = mysql_query($this->sql,$this->myDb);
			$this->numRows = mysql_num_rows($this->query);
			
			for($i=0;$i<$this->numRows;$i++)
 			{
 				$row = mysql_fetch_object($this->query);
 				
 				$this->pdfDao = new PdfDao();
 				
 				$this->pdfDao->id = $row->id;
				$this->pdfDao->description = $row->description;
				$this->pdfDao->title = $row->title;
				$this->pdfDao->url = $row->url;
				$this->pdfDao->time = $row->time;
					
				$this->result[] = $this->pdfDao;
 			}
			return $this->result;
		}catch(Exception $exc)
		{
			return $exc;
		}
		return NULL;
	}
	
	public function update(PdfDao $object) 
	{
		if($object)
		{
			try
			{
				$this->sql = "UPDATE TABLE pdf SET description='$object->description',
							title='$object->title',url='$object->url'";
				$this->query = mysql_query($this->sql,$this->myDb);
				return $this->checkQueryResult(mysql_affected_rows($this->myDb));
			}catch(Exception $exc)
			{
				return $exc;
			}
		}
		return NULL;
	}
}

?>